package com.tiantian.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tiantian.bean.PageBean;
import com.tiantian.dao.AnnouncementRepository;
import com.tiantian.datajpa.service.impl.GenericServiceImpl;
import com.tiantian.domain.Platformannouncement;
import com.tiantian.service.AnnouncementService;
import com.tiantian.util.CommonConstant.QueryType;
import com.tiantian.util.ConditionResult;

@Service
@Transactional
public class AnnouncementServiceImpl extends GenericServiceImpl<Platformannouncement, String> implements AnnouncementService {
	private AnnouncementRepository repository;

	@Autowired
	public void setRepository(AnnouncementRepository repository) {
		super.setGenericDAO(repository);
		this.repository = repository;
	}

	/**
	 * 获取公告信息
	 * 
	 * @param params
	 * @param pageBean
	 * @return
	 */
	public List<Platformannouncement> list(Map<String, String> params, PageBean pageBean) {
		ConditionResult conditionResult = getQueryCondition(params, QueryType.LIST);
		List<Platformannouncement> ret = repository.queryListBySql(conditionResult.getSql(), pageBean.getStartIndex(), pageBean.getPageSize(),
				conditionResult.getParams(), Platformannouncement.class);
		conditionResult = getQueryCondition(params, QueryType.COUNT);
		pageBean.setTotal(repository.queryCountBySql(conditionResult.getSql(), conditionResult.getParams()));
		return ret;
	}

	private ConditionResult getQueryCondition(Map<String, String> searchMap, QueryType type) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		switch (type) {
		case LIST:
			sql.append(" select * ");

			break;
		case COUNT:
			sql.append(" select count(1) ");
			break;
		default:
			break;
		}
		sql.append("from PlatformAnnouncement a ");
		sql.append("  where 1=1 ");

		if (searchMap != null) {

			if (StringUtils.isNotBlank(searchMap.get("bulletinTitle"))) {
				sql.append(" and a.bulletinTitle like :bulletinTitle ");
				params.put("bulletinTitle", "%" + searchMap.get("bulletinTitle").trim() + "%");
			}

			if (StringUtils.isNotBlank(searchMap.get("startSendTime"))) {
				sql.append(" and a.sendTime >= :startSendTime ");
				params.put("startSendTime", searchMap.get("startSendTime"));
			}
			if (StringUtils.isNotBlank(searchMap.get("endSendTime"))) {
				sql.append(" and  a.sendTime <= :endSendTime ");
				params.put("endSendTime", searchMap.get("endSendTime"));
			}

		}
		if (type == QueryType.LIST) {
			sql.append(" order by a.sendTime desc");
		}
		return new ConditionResult(sql.toString(), params);

	}

	public Map<String, Object> getAnnoucementById(String id) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();
		sql.append(" select a.*,b.username ");

		sql.append("from PlatformAnnouncement a left join PlatformManager b on b.PlatformManagerID=a.Announcer ");
		sql.append("  where 1=1 ");
		if (StringUtils.isNotBlank(id)) {
			sql.append(" and a.announcementId=:id ");
			params.put("id", id);
		}
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(sql.toString(), params);
		if (ret != null && ret.size() > 0)
			return ret.get(0);
		return null;
	}

	public String getAnnoucementId(String sendTime) {
		StringBuilder sql = new StringBuilder();
		Map<String, Object> params = new HashMap<String, Object>();

		sql.append("select max(announcementId) announcementId from PlatformAnnouncement a");
		sql.append("  where 1=1 ");
		if (StringUtils.isNotBlank(sendTime)) {
			sql.append(" and DATE_FORMAT(sendtime,'%Y-%m-%d')=:sendTime ");
			params.put("sendTime", sendTime);
		}
		List<Map<String, Object>> ret = repository.queryHumpListMapBySql(sql.toString(), params);
		if (ret != null && ret.size() > 0) {
			if (ret.get(0).get("announcementid") != null) {
				return ret.get(0).get("announcementid") + "";
			}
		}
		return null;
	}

}
